package main

import (
	"encoding/json"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	engine := gin.Default()
	engine.POST("/login", doLogin)
	engine.POST("/json", jsonMap)
	engine.Run(":9000")
}

/**
post 获取参数的方式
127.0.0.1:9000/login
{
    "name": "ruixuel",
    "password": "2013"
}
*/
func doLogin(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	c.JSON(http.StatusOK, gin.H{
		"name":     username,
		"password": password,
	})
}

func jsonMap(c *gin.Context) {
	// 获取json 中的数据
	data, _ := c.GetRawData()
	var jsonMap map[string]interface{}
	// 获取得数据转为map
	json.Unmarshal(data, &jsonMap)
	// 返回对应的map值
	c.JSON(http.StatusOK, jsonMap)
}
